package org.netty.client.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class NettyClientEncoder extends MessageToByteEncoder<Object> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object in, ByteBuf out) throws Exception {
    	
        // 开始位标记
        out.writeByte(0x02);
        // 消息长度
        String msg = in.toString();
        byte[] bytes = msg.getBytes();
        out.writeInt(bytes.length);
        // 结束位标记
        byte[] send = new byte[bytes.length + 1];
        System.arraycopy(bytes, 0, send, 0, bytes.length);
        send[send.length - 1] = 0x03;
        out.writeBytes(send);
    }

}
